<template>
    <div class="list">
    <el-row class="head">
        <el-col >——维修备件列表/报账单</el-col>
    </el-row>
    <el-row class="title">
        <el-col>维修备件列表</el-col>
    </el-row>
        <el-table
                border
                :data="partdata"
                style="width: 100%">
            <el-table-column
                    label="备件名称"
                    width="980">
                <template slot-scope="scope">
                   {{ scope.row.name }}
                </template>
            </el-table-column>
            <el-table-column
                    label="数量"
                    width="180">
                <template slot-scope="scope">
                    {{ scope.row.number }}
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            @click="handleEdit(scope.row)">补充备件</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page.sync="part.page.pageNum"
                :page-sizes="[5, 10, 15, 20]"
                :page-size="10"
                layout="sizes, prev, pager, next"
                :total="total">
        </el-pagination>
    </div>
</template>

<script>
    export default {
        name: "list",
        computed:{
            user(){
                return this.$store.getters.user;
            }
        },
        data(){
            return{
                partdata:[],
                total:0,
                part:{
                    id:'',
                    name:'',
                    number:'',
                    partAdd:{
                        partId:'',
                        adminId: '',
                        number:0,
                    },
                    page: {
                        pageNum: 1,
                        pageSize:10
                    }
                },
            }
        },
        created()
        {
            this.getParts();
        },
        methods:{
            submit(){
                this.getParts();
            },
            getParts(){
                this.$axios.post("/api/v1/admin/part/list",this.part.page).then(result => {

                    return result.data;
                })
                    .then(data1 => {
                        this.partdata=data1.data.list;
                        this.total=data1.data.total;
                    });
            },
            handleEdit(data){
                this.$prompt('请输入添加的数量', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    inputPattern: /\d+/,
                    inputErrorMessage: '格式不正确'
                }).then(({ value }) => {
                    this.part.id=data.id;
                    this.part.name=data.name;
                    this.part.number=Number(value);
                    this.part.partAdd.partId=data.id;
                    this.part.partAdd.adminId= this.user.id;
                    this.part.partAdd.number=data.number;
                    console.log(this.part);
                    this.$axios.post("/api/v1/admin/part/create",this.part).then(result => {

                        return result.data;
                    })
                        .then(data1 => {

                            this.$message({
                                type: 'success',
                                message: '你已成功添加了'
                            });

                            var i=0;
                            for(i;i<this.partdata.length;i++)
                            {
                                if (data.id === this.partdata[i].id)
                                {
                                    this.partdata[i].number=this.part.number+ this.partdata[i].number;
                                }
                            }
                        });



                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '取消添加'
                    });
                });

            },
            handleSizeChange(val) {

                this.part.page.pageSize=val;
                this.submit();
            },
            handleCurrentChange(val) {
                this.part.page.pageNum=val;
                this.submit();
            }

        }
    }
</script>

<style scoped>
    .el-input{
        width: 600px;
    }
</style>